package com.qfedu.mapper;

import com.qfedu.model.Contractor;
import com.qfedu.model.Developer;
import com.qfedu.model.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
@author LiJH
@date 2023-02-10
*/

public interface ProjectMapper {
    @Select("select count(*) from project where status=1")
    long count();
    @Select("select * from project where status=1 limit #{currentPage},#{pageSize}")
    @Results(id = "projectMap",value = {
            @Result(column = "pid",property ="pid"),
            @Result(column = "pname",property ="pname"),
            @Result(column = "starttime",property ="starttime"),
            @Result(column = "endtime",property ="endtime"),
            @Result(column = "did",property ="did"),
            //一对一查询
            @Result(column = "did",property = "developer",javaType = Developer.class,
                    one = @One(select = "com.qfedu.mapper.DeveloperMapper.findDeveByDid",fetchType = FetchType.DEFAULT)
            ),
            @Result(column = "cid",property ="cid"),
            @Result(column = "cid",property ="contractor",javaType = Contractor.class,
                    one = @One(select = "com.qfedu.mapper.ContractorMapper.findById",fetchType = FetchType.DEFAULT)
            ),
            @Result(column = "updatetime",property = "updatetime")
          })

    List<Project> findPage(@Param("currentPage") Integer currentPage,@Param("pageSize") Integer pageSize);



    @Results(id = "projectMaps",value = {
            @Result(column = "pid",property ="pid"),
            @Result(column = "pname",property ="pname"),
            @Result(column = "starttime",property ="starttime"),
            @Result(column = "endtime",property ="endtime"),
            @Result(column = "did",property ="did"),
            @Result(column = "did",property = "dname",
                    one = @One(select = "com.qfedu.mapper.DeveloperMapper.findByDid",fetchType = FetchType.DEFAULT)
            ),
            @Result(column = "cid",property ="cid"),
            @Result(column = "cid",property ="cname",javaType = Contractor.class,
                    one = @One(select = "com.qfedu.mapper.ContractorMapper.findByCid",fetchType = FetchType.DEFAULT)
            ),
            @Result(column = "updatetime",property = "updatetime")
    })
    @Select("select * from project where status=1 and pid = #{pid}")
    Project index(int pid);


    @Insert("insert into project(pname,starttime,endtime,updatetime) values(#{pname},#{starttime},#{endtime},#{updatetime}) ")
    int addProject(Project project);

    @Select("Select pid,pname,starttime,endtime,did,cid from project where status=1")
    List<Project> findAll();
    @Select("select * from project where pid=#{pid} and status=1 ")
    Project findById(int pid);

    @Update("update project set pname=#{pname},starttime=#{starttime},endtime=#{endtime} where pid=#{pid}")
    int editProject(Project project);

    //删除
    @Update("update project set status=0 where pid = #{pid}")
    int deleteByPid(int pid);
}